package com.tbs.parkingsaleapp.UI.OrderPage;

import abstergo.tbs.betterasyc.AsycBase.AsycWorker;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.tbs.parkingsaleapp.Http.JsonData.OrderInfo;
import com.tbs.parkingsaleapp.Http.Listenner.MyOrderHttper;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.Adapters.OrderAdapter;
import org.jetbrains.annotations.NotNull;

public class OrderPageFragment extends Fragment {

    static class OrderItem
    {
        private String text;
        private int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @NonNull
        @NotNull
        @Override
        public String toString() {
            return getText();
        }
    }

    public static void SpinnerReady()
    {
        Spinner spinner=root.findViewById(R.id.order_selector);
        String[] text={"默认","订单关闭","订单审核","订单交易成功","摇号成功"};
        ArrayAdapter<OrderItem> adp=new ArrayAdapter<OrderItem>(root.getContext(), R.layout.support_simple_spinner_dropdown_item);
        for(int i=0;i< text.length;i++)
        {
            OrderItem item=new OrderItem();
            item.setText(text[i]);
            item.setStatus(i-2);
            adp.add(item);
        }
        spinner.setAdapter(adp);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                OrderAdapter.OnlyFor(adp.getItem(position).status);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public static void UpdateList(Context context)
    {
        MyOrderHttper httper=new MyOrderHttper(ls);
        AsycWorker<String, OrderInfo[]> asy=new AsycWorker<>(1);
        asy.setContext(context).setBackgroundAction(httper).setResultAction(httper);
        asy.Run(SystemDataMananger.getLogined().getUserinfo().getId());
    }

    static View root;
    static ListView ls;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_order, container, false);
        ls=root.findViewById(R.id.order_list);
        if(SystemDataMananger.isLogined())
        {
            UpdateList(getContext());
        }

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}